/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.dao.hibernate;

import com.mavenrentcars.dao.ReservationDao;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Reservation;
import com.mavenrentcars.model.User;
import java.util.Date;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 * @author serg
 */
@Repository("reservationDao")
public class ReservationDaoHibernate extends GenericDaoHibernate<Reservation, Integer> implements ReservationDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    public ReservationDaoHibernate(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
        super(Reservation.class);
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Reservation> getAll(Car car, Date start_time, Date end_time) {
        List res = this.getHibernateTemplate()
                .find("from " + Reservation.class.getName()
                + " obj where obj.car = ? and obj.beginDate > ? and obj.endDate < ?",
                car, start_time, end_time);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }

    @Override
    public List<Reservation> getAll(User user) {
        List res = this.getHibernateTemplate()
                .find("from " + Reservation.class.getName()
                + " obj where obj.user = ? ",
                user);
        if (res.isEmpty()) {
            return null;
        }
        return res;
    }
}
